<?php
$objXajax->registerFunction('frmhotel_loadGrid');
$objXajax->registerFunction('frmhotel_fillDistrict');
$objXajax->registerFunction('frmhotel_fillWard');
$objXajax->registerFunction('frmhotel_init');
$objXajax->registerFunction('frmhotel_save');
$objXajax->registerFunction('frmhotel_delete');
$objXajax->registerFunction('frmhotel_edit');
function frmhotel_init(){ 
    global $objDbSelect,$lang;
    $objResponse = new xajaxResponse();
    $cbbCity = frmhotel_loadCity('frmhotel_cbbCity');    
    $objResponse->addAssign('frmhotel_tdCity','innerHTML',$cbbCity); 
    $cbbType    = frmhotel_loadHotelType('frmhotel_cbbHotelType');    
    $objResponse->addAssign('frmhotel_tdType','innerHTML',$cbbType);   
    $objResponse->addScript('xajax_frmhotel_loadGrid();');
   
    return $objResponse->getXML();   
}

function frmhotel_loadGrid(){ 
    global $objDbSelect,$lang,$path;
    $lang ='vi';
    $arrGrid = frmhotel_getGrid();    
    $objResponse = new xajaxResponse();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/main_hotel_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmhotel_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmhotel_tbl');");                              
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
 
}
function frmhotel_getGrid($user_id=''){
    global $objDbSelect;
    $user_id = $user_id==''?$_SESSION['current']['user_id']:$user_id;
    $sqlSelect = "select h.id as id, h.name as name,h.customer_code as customer_code,h.active as active,h.address as address,h.boss as boss,h.phone,
    ht.name as level,ct.name as city,dt.name as district,w.name as ward from tbl_service  as h left join tbl_service_type as ht on h.level = ht.id
    left join tbl_city as ct on h.city_id = ct.id  left join tbl_district as dt on h.district_id = dt.id 
    left join tbl_ward as w on h.ward_id = w.id   where h.user_created = '$user_id' order by h.date_update";
   //cho $sqlSelect;die();
    return $objDbSelect->GetArray($sqlSelect);  
}

function frmhotel_loadHotelType($id,$all=false){
    global $objDbSelect;
    $sql = "Select * from tbl_service_type";
    $arr = $objDbSelect->GetArray($sql);
    $strResult = '';
    for($i=0;$i<count($arr);$i++){
        $value = $arr[$i]['id'];
        $name =  $arr[$i]['name'];
        $strResult .= "<option value ='$value'>$name</option>";
    }

    $firstOption = '';
    if($all){
      $firstOption = "<option value ='' > Tất cả </option> "; 
    }else{
      $firstOption = "<option value ='' > Chọn </option> ";   
    }
    $strResult ="<select id='$id' class='width100 cbbRequired'>$firstOption".$strResult.'</select>';  
    return   $strResult;
}

function frmhotel_fillDistrict($city_id,$district_id=''){ 
    global $objDbSelect,$lang,$userid,$powerid,$powertypeid;
    $objResponse = new xajaxResponse();
    $cbbDistrict = frmhotel_loadDistrict($city_id,'frmhotel_cbbDistrict',$district_id);    
    $objResponse->addAssign('frmhotel_tdDistrict', 'innerHTML',$cbbDistrict);   
    return $objResponse->getXML();   
}
function frmhotel_fillWard($district_id,$ward_id=''){ 
    global $objDbSelect,$lang,$userid,$powerid,$powertypeid;
    $objResponse = new xajaxResponse();
    $cbbWard = frmhotel_loadWard($district_id,'frmhotel_cbbWard',$ward_id);  
    $objResponse->addAssign('frmhotel_tdWard', 'innerHTML',$cbbWard);   
    return $objResponse->getXML();   
}

/*
* Hàm dùng để tạo chuổi combobox cho loại quyền.
* @param 1 : $powertypeid - Loại quyền của user đăng nhập
* @param 2 : tên hàm đ 
*/
function frmhotel_loadCity($id,$all=false){
    global $objDbSelect,$lang;
    $condition = '';
    $powertypeid =1;
    $sql =  " select id as id, name as name from tbl_city where active = 1 ";
    $arr = $objDbSelect->GetArray($sql);
    $strResult = '';
    for($i=0;$i<count($arr);$i++){
        $value = $arr[$i]['id'];
        $name =  $arr[$i]['name'];
        $strResult .= "<option value ='$value'>$name</option>";
    }

    $firstOption = '';
    if($all){
      $firstOption = "<option value ='' > Tất cả </option> "; 
    }else{
      $firstOption = "<option value ='' > Chọn </option> ";   
    }
    $strResult ="<select id='$id' class='width100 cbbRequired'>$firstOption".$strResult.'</select>';  
    return   $strResult;
}
function frmhotel_loadDistrict($city_id,$id,$district_id=''){
    global $objDbSelect,$lang;

    $condition = '';
    $powertypeid =1;
    $sql =  " select id as id, name as name from tbl_district where active = 1 and city_id = '$city_id'";
    $arr = $objDbSelect->GetArray($sql);
    $strResult = '';
    for($i=0;$i<count($arr);$i++){
        $value = $arr[$i]['id'];
        $name =  $arr[$i]['name'];
        $addselect='';
        if($value==$district_id){
            $addselect = ' selected="selected" ';
        }
        $strResult .= "<option value ='$value' $addselect>$name</option>";
    }
    $firstOption = "<option value ='' > Chọn </option> ";   
    $strResult ="<select id='$id' class='cbbRequired width100'>$firstOption".$strResult.'</select>';
     
    return $strResult;       
}
function frmhotel_loadWard($district_id,$id,$ward_id=''){
    global $objDbSelect,$lang;
    $condition = '';
    $powertypeid =1;
    $sql =  " select id as id, name as name from tbl_ward where active = 1 and district_id = '$district_id'";
    $arr = $objDbSelect->GetArray($sql);
    $strResult = '';
    for($i=0;$i<count($arr);$i++){
        $value = $arr[$i]['id'];
        $name =  $arr[$i]['name'];
        $addselect='';
        if($value==$ward_id){
            $addselect = ' selected="selected" ';
        }
        $strResult .= "<option value ='$value' $addselect>$name</option>";
    }
    $firstOption = "<option value =''> Chọn </option> ";   
    $strResult ="<select id='$id' class='width100 cbbRequired'>$firstOption".$strResult.'</select>';
     
    return $strResult;       
}
function frmhotel_save($params=null)
{
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $name = addslashes($params['txthotelname']);
    $boss = addslashes($params['boss']);
    $level = $params['cbbHotelType'];
    $city_id = $params['cbbCity'];
    $district_id = $params['cbbDistrict'];
    $ward_id = $params['cbbWard'];
    $address = $params['txtAddress'];
    $description = $params['note'];
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $phone = $params['txtPhone'];
    $result = true;      
    if($edit==1){
        $sql = "update tbl_service set name='$name',level='$level',city_id='$city_id',district_id='$district_id',phone='$phone',
        ward_id='$ward_id',address='$address',boss='$boss',date_update=now() WHERE id = '$id'";
        $result = $objDbSelect->Execute($sql);
        
    }else{
        $order_hotel = frmhotel_getmaxOrder(); 
       $order_prefix=  str_pad($order_hotel, 4, "0", STR_PAD_LEFT); 
       $customer_code = $_SESSION['current']['user_name'].$order_prefix;
        $user_id = $_SESSION['current']['user_id'];
        $sqltbl_postfix = "select tbl_postfix, count(tbl_postfix) as num from tbl_service group by tbl_postfix order by num asc, tbl_postfix desc";
        $arrpostfix = $objDbSelect->GetArray($sqltbl_postfix);
        $tbl_postfix =1;
        if(count($arrpostfix)>0){
            if($arrpostfix[0]['num']>=MAXHOTEL){
                $tbl_postfix = (int)$arrpostfix[0]['tbl_postfix'] +1;
            }else{
               $tbl_postfix = (int)$arrpostfix[0]['tbl_postfix']; 
            }
        }
        
        $sql = " INSERT INTO tbl_service(name,level,city_id,district_id,ward_id,address,phone,
        date_created,user_created,date_update,user_update,order_hotel,customer_code,boss,tbl_postfix)
        value('$name','$level','$city_id','$district_id','$ward_id','$address','$phone',now(),'$user_id',now(),'$user_id',
        '$order_hotel','$customer_code','$boss','$tbl_postfix') ";
        $result = $objDbSelect->Execute($sql);
        $id_insert = mysql_insert_id();
        frmhotel_drop($id_insert);
        frmhotel_createtable($id_insert);
        $objResponse->addScript("changeUpdate('frmhotel',$id_insert);");        
        if($result){
            $result=frmhotel_createrCustomer($customer_code,$level,$id_insert,$phone);
        }
    }
    $objResponse->addScript('xajax_frmhotel_loadGrid();');
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();
}

function frmhotel_createtable($table_id){
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $sql = "SELECT * FROM tbl_tablescript WHERE  active = 1";
    $arr = $objDbSelect->GetArray($sql);
    $output ='';
    for($i=0;$i<count($arr);$i++){
        $table_name = $arr[$i]['table_name'];  
        $table_script = $arr[$i]['table_script']; 
        $sqlcreate =  str_replace($table_name,$table_name.'_'.$table_id,$table_script) ;
        $result = $objDbSelect->Execute($sqlcreate);
        if(!$result){
        $output .=  ",$table_name".'_'.$table_id; // dùng để biết bàn nào tạo không được
        }          
    }    

    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();    
    // return $output;
}

function frmhotel_drop($table_id){
    global $objDbSelect;
    $sql = "SELECT * FROM tbl_tablescript WHERE active = 1";
    $arr = $objDbSelect->GetArray($sql);
    $output ='';

    for($i=0;$i<count($arr);$i++){
        $table_name = $arr[$i]['table_name'];  
        $sqldrop = "DROP TABLE IF EXISTS $table_name"."_$table_id";
        $result = $objDbSelect->Execute($sqldrop);
        if(!$result){
          $output .=  ",$table_name_".$table_id;
        }          
    }      
}

function frmhotel_createrCustomer($username,$level,$hotel_id,$phone=''){
    global $objDbSelect,$arrPostFix;
    $arrPos = $arrPostFix[$level];
    $pass = EncryptPass(PASS_DEFAULT);   
    $power_type_cus = POWER_CUSTOMER; 
    $power_type_subcus = POWER_SUB_CUSTOMER; 
    $userid = $_SESSION['current']['user_id'];
    $sql = " insert into tbl_user(username,pass,power_type_id,phone,active,hotel_id,user_created)
    values('$username','$pass',$power_type_cus,'$phone',1,'$hotel_id','$userid')";
    $addValue='';
    foreach($arrPos as $value){
        $usernamesub = $username.$value;
        $addValue .=",('$usernamesub','$pass',$power_type_subcus,'',0,'$hotel_id','$userid')";
    }
    $sql.= $addValue;    
    if($objDbSelect->Execute($sql)){
        return true;
    }else{
        return false;
    }
    
}
function frmhotel_getmaxOrder($agent_id=''){
    global $objDbSelect;
    if($agent_id==''){
       $agent_id = $_SESSION['current']['user_id']; 
    }
    $sql = "select max(order_hotel) + 1 as  order_hotel from tbl_service where user_created = $agent_id";
    $arr =  $objDbSelect->GetArray($sql);
    if(count($arr)==0){
        return 1;
    }
    $result =  $arr[0]['order_hotel']==''?1:$arr[0]['order_hotel'];
    //echo count($arr);die();
    
    return $result;
}
function frmhotel_delete($id){
    global $objDbSelect;
    $sql = "select id, customer_code from tbl_service where id in ($id)";
    $arr = $objDbSelect->GetArray($sql);
    for($i=0;$i<count($arr);$i++){
        $customer_code = $arr[$i]['customer_code'];
        $id = $arr[$i]['id'];
        $sqldelete = "delete from tbl_user where username LIKE '$customer_code%'" ;  
        if($objDbSelect->Execute($sqldelete)){
           $sqldelete = "delete from tbl_service where id = '$id'" ;
           $objDbSelect->Execute($sqldelete);
        }   
    }
    $objResponse = new xajaxResponse();
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();
}

function frmhotel_edit($id){
     global $objDbSelect;
      $objResponse = new xajaxResponse();
    $sql = "select * from tbl_service where id in ($id)";
    $arr = $objDbSelect->GetArray($sql);
    $hotel_name = $arr[0]['name'];
    $hotel_type = $arr[0]['level'];
    $city = $arr[0]['city_id'];
    $district = $arr[0]['district_id'];
    $ward = $arr[0]['ward_id'];
    $address = $arr[0]['address'];
    $boss = $arr[0]['boss'];
    $phone = $arr[0]['phone'];
    
    $objResponse->addScript("$('#frmhotel_txthotelname').val('$hotel_name')");
    $objResponse->addScript("$('#frmhotel_cbbHotelType').val('$hotel_type')");
    $objResponse->addScript("$('#frmhotel_cbbCity').val('$city')");
    $objResponse->addScript("$('#frmhotel_cbbDistrict').val('$district')");
    $objResponse->addScript("$('#frmhotel_cbbWard').val('$ward')");
    $objResponse->addScript("$('#frmhotel_txtAddress').val('$address')");
    $objResponse->addScript("$('#frmhotel_boss').val('$boss')");
    $objResponse->addScript("$('#frmhotel_txtPhone').val('$phone')");
    $objResponse->addScript("xajax_frmhotel_fillDistrict($city,$district)");
    $objResponse->addScript("xajax_frmhotel_fillWard($district,$ward)");
   
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();
}

?>
